package com.miaoxun.service.impl;


import com.basemapper.BasicMapper;
import com.miaoxun.dao.DeptMapper;
import com.miaoxun.domain.entity.Dept;
import com.miaoxun.domain.model.Tree;
import com.miaoxun.service.DeptService;
import com.miaoxun.service.util.BuildTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class DeptServiceImpl extends BaseServiceImpl<Dept> implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    protected BasicMapper<Dept> getMapper() {
        return deptMapper;
    }





    @Override
    public Tree<Dept> getTree() {
        List<Tree<Dept>> trees = new ArrayList<Tree<Dept>>();
        List<Dept> sysDepts = getAll();
        for (Dept sysDept : sysDepts) {
            Tree<Dept> tree = new Tree<Dept>();
            tree.setId(sysDept.getDeptId().toString());
            tree.setParentId(sysDept.getParentId().toString());
            tree.setText(sysDept.getName());
            Map<String, Object> state = new HashMap<String, Object>(16);
            state.put("opened", true);
            tree.setState(state);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        return BuildTree.build(trees);
    }

    @Override
    public int count(Long parentId) {
        return deptMapper.count(parentId);
    }

    @Override
    public int checkDeptHasUser(Long parentId) {
        return deptMapper.checkDeptHasUser(parentId);
    }
}
